﻿var step = 1;
var reg_json = "";
var hashTable;

function nextStep() {
    switch (step) {
        case 1:
            {
                $("#regTitle").text(": step " + step + " di 3");
                $("#step_4").hide();
                hashTable = new Array();
                $(".prev_button").hide();
                $(".next_button").show();
                //remove all the rules
                $("#email").rules("remove");
                $("#email2").rules("remove");
                $("#password").rules("remove");
                $("#password2").rules("remove");
                $("#altro").rules("remove");
                $("#agree").rules("remove");

                $("#nickname").rules("add", {
                    required: true,
                    minlength: 4,
                    maxlength: 10,
                  remote: {
                  url: "Registration.aspx",
                        type: "get",
                        data: {
                            nickname: function() {
                                var manda = '{"event": "nickname","value":"' + $("#nickname").val().toLowerCase() + '"}';
                                return manda;
                            }
                        }
                    },
                    messages: {
                       remote: "Il nickname scelto è occupato, cambialo",
                        required: "Campo obbligatorio",
                        minlength: jQuery.format("Almeno {0} caratteri"),
                        maxlength: jQuery.format("Massimo {0} caratteri")
                    }
                });
                $("#firstname").rules("add", {
                    required: true,
                    minlength: 2,
                    maxlength: 12,
                    messages: {
                        required: "Campo obbligatorio",
                        minlength: jQuery.format("Almeno {0} caratteri"),
                        maxlength:jQuery.format("Massimo {0} caratteri")
                    }
                });
                $("#lastname").rules("add", {
                    required: true,
                    minlength: 2,
                    maxlength: 12,
                    messages: {
                        required: "Campo obbligatorio",
                        minlength: jQuery.format("Almeno {0} caratteri"),
                        maxlength: jQuery.format("Massimo {0} caratteri")
                    }
                });
                $("#birthdate").rules("add", {
                    required: false,
                    italianDate: true
                });
                $("#birthdate").datepicker({
                    changeMonth: true,
                    initStatus: 'Seleziona una data',
                    yearRange: '1970:2000',
                    changeYear: true,
                    defaultDate: '-25y'
                });
                
                $("#reg_step").show();
                $("#step_2").hide();
                $("#step_3").hide();
                $("#info_field").css("border-color", "#95de2d");
                $("#step_1").show();
                $("#nickname").focus();
                step++;
            }
            break;
        case 2:
            {
                hashTable["nickname"] = $("#nickname").val().toString();
                hashTable["firstname"] = $("#firstname").val().toString();
                hashTable["lastname"] = $("#lastname").val().toString();
                hashTable["birthdate"] = $("#birthdate").val().toString();
                $(".prev_button").show();
                $("#step_1").hide();
                $("#step_3").hide();
                $("#regTitle").text(": step " + step + " di 3");
                $("#agree").rules("remove");

                //add rule to check password
                $("#password").rules("add", {
                    required: true,
                    minlength: 6,
                    messages: {
                        required: "Password obbligatoria",
                        minlength: jQuery.format("Almeno {0} caratteri richiesti")
                    }
                });
                //add rule to check password
                $("#password2").rules("add", {
                    required: true,
                    equalTo: "#password",
                    messages: {
                        required: "Password obbligatoria",
                        equalTo: "Le 2 password devono coincidere"
                    }
                });
                //add rule to check email
                $("#email").rules("add", {
                    required: true,
                    email: true,
                    remote: {
                    url: "Registration.aspx",
                        type: "get",
                        data: {
                            email: function() {
                                var manda = '{"event": "email","value":"' + $("#email").val().toLowerCase() + '"}';
                                return manda;
                            }
                        }
                    },
                    messages: {
                        remote: "Indirizzo email già in uso",
                        required: "Indirizzo email obbligatorio",
                        email: "Formato indirizzo email non valido"
                    }
                });
                //add rule to check email
                $("#email2").rules("add", {
                    required: true,
                    email: true,
                    equalTo: "#email",
                    messages: {
                        required: "Indirizzo email obbligatorio",
                        equalTo: "I due indirizzi email devono coincidere",
                        email: "Formato indirizzo email non valido"
                    }
                });
                $("#info_field").css("border-color", "#fad82e");
                $("#step_2").fadeIn("slow");
                $("#password").focus();
                step++;
            }
            break;
        case 3:
            {
                hashTable["password"] = MD5($("#password").val());
                hashTable["email"] = $("#email").val().toString();
                $("#step_2").hide();
                $("#regTitle").text(": step " + step + " di 3");
                $("#agree").rules("add", {
                    required: true,
                    messages: {
                        required: "Campo obbligatorio"
                    }
                });
                $("#info_field").css("border-color", "#eb008a");
                $("#step_3").fadeIn("slow");
                $("#nationality").focus();
                step++;
            }
            break;
        case 4:
            {
                hashTable["nationality"] = $("#nationality").val();

                if ($("#where").val() == "Altro") {
                    hashTable["where"] = $("#altro").val();
                }
                else {
                    hashTable["where"] = $("#where").val();
                }

                reg_json = '{"event":"registration","nickname":"' +
               hashTable["nickname"] + '","firstname":"' + hashTable["firstname"] +
               '","lastname":"' + hashTable["lastname"] + '","birthdate":"' +
               hashTable["birthdate"] + '","password":"' + hashTable["password"] +
               '","email":"' + hashTable["email"] + '","nationality":"' + hashTable["nationality"] + '","where":"' + hashTable["where"] + '"}';

                $.post("Registration.aspx", reg_json, function(response) {

                    if (response) {
                        step++;
                        nextStep();
                        myForm.resetForm();

                    } else alert("Errore con la registrazione, ritenta!");
                });
            }
            break;
        case 5:
            {
                $("#step_3").hide();
                $(".prev_button").hide();
                $(".next_button").hide();
                $("#regTitle").text(" completata");
                $("#step_4").fadeIn("slow");
                $("#step_4").html(successfulRegistration);
                step = 1;
            }
    }
}

    $(document).ready(function() {

    $.getScript("script/plugin/jquery.validate.min.js", function() {
    $.getScript("script/plugin/validate.js", function() {
        
    regAttempts++;

    //alert("tentativo: " + regAttempts);

    if (regAttempts == 1) {
        $(".prev_button").click(function() {
            step -= 2;
            // alert("Indietro su " + step);
            nextStep();
        });

        $("#where").change(function() {
            if ($(this).val() == "Altro")
                $("#altro").show();

                $("#altro").rules("add", {
                required: true,
                maxlength: 25,
                    messages: {
                        required: "Campo obbligatorio",
                        maxlength: jQuery.format("Massimo {0} caratteri")
                    }
                });
        });

        //  myRegForm.resetForm();
        nextStep();
    }
    else {
        step = 1;
        nextStep();
    }



    $.datepicker.regional['it'] = {
        prevText: '&lt;Prec', prevStatus: '',
        prevStatus: 'Mostra il mese precedente',
        nextText: 'Succ&gt;', nextStatus: '',
        nextStatus: 'Mostra il mese successivo',
        currentText: 'Oggi', currentStatus: '',
        currentStatus: 'Mostra il mese corrente',
        monthNames: ['Gennaio', 'Febbraio', 'Marzo', 'Aprile', 'Maggio', 'Giugno',
                'Luglio', 'Agosto', 'Settembre', 'Ottobre', 'Novembre', 'Dicembre'],
        monthNamesShort: ['Gen', 'Feb', 'Mar', 'Apr', 'Mag', 'Giu',
                'Lug', 'Ago', 'Set', 'Ott', 'Nov', 'Dic'],
        monthStatus: 'Mostra un altro mese',
        yearStatus: 'Mostra un altro anno',
        weekHeader: 'Sm', weekStatus: '',
        weekStatus: 'Settimana dell\'anno',
        dayNames: ['Domenica', 'Luned&#236', 'Marted&#236', 'Mercoled&#236', 'Gioved&#236', 'Venerd&#236', 'Sabato'],
        dayNamesShort: ['Dom', 'Lun', 'Mar', 'Mer', 'Gio', 'Ven', 'Sab'],
        dayNamesMin: ['Do', 'Lu', 'Ma', 'Me', 'Gio', 'Ve', 'Sa'],
        dayStatus: 'Imposta DD come primo giorno della settimana',
        dateStatus: 'Seleziona DD, M d',
        dateFormat: 'dd/mm/yy', firstDay: 1,
        isRTL: false
    };
    $.datepicker.setDefaults($.datepicker.regional['it']);
        });
    });
});